﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;


namespace WebSite.admin.company
{
    public partial class SalesmanRelation : BaseAdmin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSalesman();
            }

        }

        protected void ddlSalesman_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlSalesman.SelectedItem.Value == string.Empty) return;
            string sql = "select distinct c.Id,  c.[Name] as CompanyName from DB_adminlogin a "
                        + " join tbCompanyAdmin ca on a.AdminId = ca.AdminId  join tbCompany c on ca.CompanyId = c.Id and a.AdminTypeId = 1 "
                          + "and c.id in (select c.id from tbCompany c join tbCompanyAdmin ca on c.Id=ca.CompanyId and isnull(ca.AdminTypeId,0)=0  and ca.AdminId = " + this.ddlSalesman.SelectedItem.Value + ")";
            string sql2 = "select id, [Name] as CompanyName from tbCompany where isnull(isDeleted,0) = 0 "
                        + "and parentId in (select c.id from tbCompany c join tbCompanyAdmin ca on c.Id=CompanyId and ca.AdminTypeId=1  and ca.AdminId = " + this.ddlSalesman.SelectedItem.Value + ")"
                        + " order by [Name] ";

            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            DataTable tableLeft = SQLAccess.GreatDs(sql2).Tables[0];

            bool isRight = false;
            foreach (DataRow dr in tableLeft.Rows)
            {
                isRight = false;
                foreach (DataRow drRight in table.Rows)
                {
                    if (dr["Id"].ToString() == drRight["Id"].ToString())
                    {
                        this.lbRight.Items.Add(new ListItem(dr["CompanyName"].ToString(), dr["Id"].ToString()));
                        isRight = true;
                        break;
                    }
                }
                if (isRight == false)
                {
                    this.lbLeft.Items.Add(new ListItem(dr["CompanyName"].ToString(), dr["Id"].ToString()));
                }

            }
        }

        protected void btnToAllRight_Click(object sender, EventArgs e)
        {
            foreach (ListItem itm in this.lbLeft.Items)
            {
                this.lbRight.Items.Add(itm);
            }
            this.lbLeft.Items.Clear();
        }

        protected void btnToAllLeft_Click(object sender, EventArgs e)
        {
            foreach (ListItem itm in this.lbRight.Items)
            {
                this.lbLeft.Items.Add(itm);
            }
            this.lbRight.Items.Clear();
        }

        protected void btnToRight_Click(object sender, EventArgs e)
        {
            if (this.lbLeft.SelectedIndex >= 0)
            {
                this.lbRight.Items.Add(this.lbLeft.SelectedItem);
                this.lbLeft.Items.RemoveAt(this.lbLeft.SelectedIndex);
            }

        }
        protected void btnToLeft_Click(object sender, EventArgs e)
        {
            if (this.lbRight.SelectedIndex >= 0)
            {
                this.lbLeft.Items.Add(this.lbRight.SelectedItem);
                this.lbRight.Items.RemoveAt(this.lbRight.SelectedIndex);
            }

        }

        void BindSalesman()
        {
            string sql = "select a.adminId,a.AdminUser from DB_adminlogin a join tbCompanyAdmin ca on a.AdminId = ca.AdminId and a.AdminTypeId = 1 and ca.AdminTypeId = 1 and ca.CompanyId = " + Request.QueryString["cid"] + " order by AdminUser";
            DataTable table = SQLAccess.GreatDs(sql).Tables[0];
            this.ddlSalesman.DataTextField = "AdminUser";
            this.ddlSalesman.DataValueField = "AdminId";
            this.ddlSalesman.DataSource = table;
            this.ddlSalesman.DataBind();
            this.ddlSalesman.Items.Insert(0, new ListItem("请选择业务员", string.Empty));
            if (Request.QueryString["uid"] != "")
            {
                this.ddlSalesman.Items.FindByValue(Request.QueryString["uid"]).Selected = true;
                ddlSalesman_SelectedIndexChanged(null, null);
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (this.ddlSalesman.SelectedItem.Value != string.Empty)
            {
                StringBuilder sql = new StringBuilder();
                sql.AppendFormat("delete from tbCompanyAdmin where AdminId in(select AdminId from DB_adminlogin where AdminTypeId = 1) and AdminId = {0} and isnull(AdminTypeId,0) = 0 ;", this.ddlSalesman.SelectedItem.Value);
                foreach (ListItem item in this.lbRight.Items)
                {
                    sql.AppendFormat(" insert into tbCompanyAdmin(CompanyId,AdminId) values({0},{1});"
                                     , item.Value
                                     , this.ddlSalesman.SelectedItem.Value
                                   );

                }

                SQLAccess.DoSql(sql.ToString());

                Response.Write("<script>alert('保存成功！');window.location.href='CompanyUserList.aspx?id=" + Request.QueryString["cid"] +"';</script>");
            }
        }

    }
}